package cn.cloud.all.security.web.savedrequest;

import java.util.*;

public class Enumerator<T> implements Enumeration<T> {

    private Iterator<T> iterator;

    public Enumerator(Collection<T> collection) {
        this(collection.iterator());
    }

    public Enumerator(Collection<T> collection, boolean clone) {
        this(collection.iterator(), clone);
    }

    public Enumerator(Iterator<T> iterator) {
        this.iterator = iterator;
    }

    public Enumerator(Iterator<T> iterator, boolean clone) {
        if (!clone) {
            this.iterator = iterator;
        } else {
            List<T> list = new ArrayList<>();
            while (iterator.hasNext()) {
                list.add(iterator.next());
            }
            this.iterator = list.iterator();
        }
    }

    public Enumerator(Map<?, T> map) {
        this(map.values().iterator());
    }

    public Enumerator(Map<?, T> map, boolean clone) {
        this(map.values().iterator(), clone);
    }

    public boolean hasMoreElements() {
        return (iterator.hasNext());
    }

    public T nextElement() throws NoSuchElementException {
        return (iterator.next());
    }
}
